package leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 描述:572. 另一棵树的子树
 *
 * @author Madison You
 * @created 13:05
 */
public class Easy_572_IsSubtree {
    public static void main(String[] args) {
        Integer[] arr1 = {3, 4, 5, 1, 2};
        Integer[] arr2 = {4,1,2};
        TreeNode treeNode1 = CreateTreeNode.arrayToTreeNode(arr1);
        TreeNode treeNode2 = CreateTreeNode.arrayToTreeNode(arr2);
        new Easy_572_IsSubtree().isSubtree(treeNode1, treeNode2);
    }

    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null) {
            return false;
        }
        return isSameTree(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        } else if (p == null || q == null) {
            return false;
        } else if (p.val != q.val) {
            return false;
        } else {
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        }
    }
}
